import { refuseApplyApi } from "@/api/financialManagement/payableManagement/paymentApproval";
import FormHook from "@/hook/formHook";
import { message } from "ant-design-vue";
import { Ref } from "vue";
export default function OperationHook(state: {
  type: Ref<number>;
  formState: { applyResult: string | undefined };
  formDataRef: Ref<any>;
  emit: any;
  passData: Ref<PaymentApproval.ListRows[]>;
  loading: Ref<boolean>;
}) {
  const { formState, formDataRef, emit, passData, loading } = state;
  const { resetFields } = FormHook({ formState });
  const submitHandle = () => {
    formDataRef.value.validate().then(() => {
      const params: { idList: Array<string>; applyResult: string | undefined } = {
        idList: passData.value.map((t: any) => t.id),
        applyResult: formState.applyResult
      };
      loading.value = true;
      refuseApplyApi(params)
        .then((res: any) => {
          const { code } = res;
          if (code === 200) {
            message.success("驳回成功!");
            emit("submit");
          }
        })
        .finally(() => (loading.value = false));
    });
  };
  const resetHandle = () => {
    resetFields();
    formDataRef.value.clearValidate();
    emit("update:visible", false);
  };
  return {
    submitHandle,
    resetHandle,
    resetFields
  };
}
